package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    @Override
    public PageResult getDishList(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOPage = dishMapper.selectDishList(dishPageQueryDTO);

        return new PageResult(dishVOPage.getTotal(), dishVOPage.getResult());
    }

    @Override
    @Transactional
    public void insertDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        Long id = dish.getId();
        // 完善--添加口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> flavor.setDishId(id));
            dishFlavorMapper.insert(flavors);
        }
//        log.info("flavors:{}", flavors);
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dish = dishMapper.selectById(id);
        List<DishFlavor> flavors = dishFlavorMapper.selectById(id);
        dish.setFlavors(flavors);
        return dish;
    }

    @Transactional
    @Override
    public void deleteDish(List<Long> dishIdList) {
        // 判断商品是否启售
        dishIdList.forEach(dishId -> {
            DishVO dishVO = dishMapper.selectById(dishId);
            if(dishVO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // 判断商品是否关联套餐
        Long count = setMealDishMapper.selectIsExist(dishIdList);
        if(count != 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除口味
        dishFlavorMapper.delete(dishIdList);

        // 删除基础信息
        dishMapper.delete(dishIdList);

    }

    @Override
    @Transactional
    public void changeStatus(Integer status, Long dishId) {
        Long setMealId = setMealDishMapper.getSetMealIdByDishId(dishId);
        if(status == 0) {
            setMealMapper.update(Setmeal.builder().status(status).id(setMealId).build());
        }

        Dish dish = Dish.builder().status(status).id(dishId).build();
        dishMapper.update(dish);
    }

    @Override
    public List<Dish> getDishByCategory(Long categoryId) {
        List<Dish> dishList =  dishMapper.selectByCategory(categoryId);

        return dishList;
    }

    @Override
    public List<DishVO> userGetDishByCategory(Long categoryId) {
        List<DishVO> dishList = dishMapper.userSelectByCategory(categoryId);
        dishList.forEach(dish -> {
            List<DishFlavor> flavors = dishFlavorMapper.selectById(dish.getId());
            dish.setFlavors(flavors);
        });

        return dishList;
    }


    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        // 修改基础信息
        dishMapper.update(dish);

        // 修改口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && !flavors.isEmpty()) {
            // 先删除
            List<Long> dishIdList = new ArrayList<>();
            dishIdList.add(dishDTO.getId());
            dishFlavorMapper.delete(dishIdList);
            // 再新增
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insert(flavors);
        }
    }


}
